package com.mg.controller;

import com.github.pagehelper.PageInfo;
import com.mg.common.Constants;
import com.mg.dto.PurchaseSaleDto;
import com.mg.dto.SimpleResult;
import com.mg.model.PurchaseSale;
import com.mg.model.User;
import com.mg.service.PurchaseSaleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * Created by mg1213 on 2018/3/18.
 */
@Controller
@RequestMapping("/purchaseSale")
public class PurchaseSaleController {
    @Autowired
    private PurchaseSaleService purchaseSaleService;

    @RequestMapping(value = "/admin/selectPageInfo")
    @ResponseBody
    public PageInfo<PurchaseSaleDto> selectPageInfo(PurchaseSaleDto dto, int limit, int start) {
        return purchaseSaleService.selectPageInfo(dto, start, limit);
    }

    @RequestMapping(value = "/admin/insertPurchaseSale")
    @ResponseBody
    public SimpleResult insertPurchaseSale(PurchaseSale record) {
        return purchaseSaleService.insertPurchaseSale(record);
    }

    @RequestMapping(value = "/user/insertPurchaseSale")
    @ResponseBody
    public SimpleResult insertPurchaseSale1(PurchaseSale record, HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute(Constants.USER_INFO);
        record.setUserId(user.getId());
        return purchaseSaleService.insertPurchaseSale(record);
    }
    @RequestMapping(value = "/user/getPurchaseSale")
    @ResponseBody
    public PurchaseSaleDto getPurchaseSale(PurchaseSaleDto record) {
        return purchaseSaleService.getPurchaseSale(record);
    }
    @RequestMapping(value = "/user/getDetail")
    @ResponseBody
    public Map<String,Object> getDetail(PurchaseSaleDto record, HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute(Constants.USER_INFO);
        return purchaseSaleService.getDetail(record,user.getId());
    }
    @RequestMapping(value = "/user/tradeContract")
    @ResponseBody
    public SimpleResult tradeContract(Integer id, BigDecimal num, HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute(Constants.USER_INFO);
        return purchaseSaleService.tradeContract(id,user.getId(),num);
    }


    @RequestMapping(value = "/admin/updatePurchaseSale")
    @ResponseBody
    public SimpleResult updatePurchaseSale(PurchaseSale record) {
        return purchaseSaleService.updatePurchaseSale(record);
    }

    @RequestMapping(value = "/admin/deletePurchaseSale")
    @ResponseBody
    public SimpleResult deletePurchaseSale(String ids) {
        return purchaseSaleService.deletePurchaseSale(ids);
    }

}
